package com.ruoyi.quartz.service.impl;

import com.ruoyi.bl.domain.BlAppParams;
import com.ruoyi.bl.domain.BlDeliveryDoc;
import com.ruoyi.bl.domain.BlDeliveryLog;
import com.ruoyi.bl.domain.BlOrder;
import com.ruoyi.bl.mapper.BlDeliveryDocMapper;
import com.ruoyi.bl.mapper.BlDeliveryLogMapper;
import com.ruoyi.bl.mapper.BlOrderMapper;
import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.mapper.SysJobMapper;
import com.ruoyi.quartz.mapper.SysMyJobMapper;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.quartz.service.ISysMyJobService;
import com.ruoyi.quartz.util.CronUtils;
import com.ruoyi.quartz.util.ScheduleUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.BlockingDeque;

/**
 * 定时任务调度信息 服务层
 * 
 * @author ruoyi
 */
@Service
public class SysMyJobServiceImpl implements ISysMyJobService
{
    @Autowired
    private Scheduler scheduler;

    @Autowired
    private SysJobMapper jobMapper;

    @Autowired
    private SysMyJobMapper myJobMapper;

    @Autowired
    private BlOrderMapper orderMapper;

    @Autowired
    private BlDeliveryDocMapper deliveryDocMapper;

    @Autowired
    private BlDeliveryLogMapper deliveryLogMapper;

    /**
     * 项目启动时，初始化定时器 主要是防止手动修改数据库导致未同步到定时任务处理（注：不能手动修改数据库ID和任务组名，否则会导致脏数据）
     */
    @PostConstruct
    public void init() throws SchedulerException, TaskException
    {
        scheduler.clear();
        List<SysJob> jobList = jobMapper.selectJobAll();
        for (SysJob job : jobList)
        {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
    }


    @Override
    public int updateBlOrderAndStore() {
        return myJobMapper.updateBlOrderAndStore();
    }

    @Override
    public int updateBlCartAndStore() {
        return myJobMapper.updateBlCartAndStore();
    }

    @Override
    public int updateBlStoreAbnormal() {
        return myJobMapper.updateBlStoreAbnormal();
    }

    @Override
    public void assignDeliveryUser() {
        BlOrder blOrder = new BlOrder();
        blOrder.setDeliveryId((long)0);
        blOrder.setOrderStatus(0);
        blOrder.setPayStatus(1);
        List<BlOrder> orderList = orderMapper.selectBlOrderList(blOrder);
        long psUserId = 0;
        for(int i = 0;i<orderList.size(); i++){
            BlOrder blOrderTmp = orderList.get(i);
            BlDeliveryDoc bddQuery = new BlDeliveryDoc();
            bddQuery.setDeptId(blOrderTmp.getDeptId());
            BlDeliveryDoc blDeliveryDoc = deliveryDocMapper.findBlDeliveryAssignUser(bddQuery);
            psUserId = blDeliveryDoc.getPsuserId();
            BlDeliveryDoc blDeliveryDocTmp = new BlDeliveryDoc();
            blDeliveryDocTmp.setDeptId(blOrderTmp.getDeptId());
            blDeliveryDocTmp.setOrderId(blOrderTmp.getOrderId());
            blDeliveryDocTmp.setOrderSn(blOrderTmp.getOrderSn());
            blDeliveryDocTmp.setUserId(blOrderTmp.getUserId());
            blDeliveryDocTmp.setPsuserId(blDeliveryDoc.getPsuserId());
            blDeliveryDocTmp.setConsignee(blOrderTmp.getConsignee());
            blDeliveryDocTmp.setMobile(blOrderTmp.getMobile());
            blDeliveryDocTmp.setPsStatus((long)0);
            blDeliveryDocTmp.setAddress(blOrderTmp.getAddress());
            if(blOrderTmp.getExpectedTime() != null){
                blDeliveryDocTmp.setBestTime(blOrderTmp.getExpectedTime());
            }
            blDeliveryDocTmp.setNote(blOrderTmp.getUserNote());
            deliveryDocMapper.insertBlDeliveryDoc(blDeliveryDocTmp);
            blOrderTmp.setDeliveryId(psUserId);
            orderMapper.updateBlOrder(blOrderTmp);
            BlDeliveryLog blDeliveryLog = new BlDeliveryLog();
            blDeliveryLog.setDeliveryId(blOrderTmp.getDeliveryId());
            blDeliveryLog.setDeptId(blOrderTmp.getDeptId());
            blDeliveryLog.setUserId(blOrderTmp.getUserId());
            blDeliveryLog.setOrderId(blOrderTmp.getOrderId());
            blDeliveryLog.setPsuserId(psUserId);
            deliveryLogMapper.insertBlDeliveryLog(blDeliveryLog);
        }
    }

    @Override
    public void assignTimeoutDeliveryUser() {
        List<BlDeliveryDoc> blDeliveryDocList = deliveryDocMapper.findBlDeliveryTimeout(new BlDeliveryDoc());
        for(int i=0;i<blDeliveryDocList.size();i++){
            long deliveryId = blDeliveryDocList.get(i).getDeliveryId();
            long orderId = blDeliveryDocList.get(i).getOrderId();
            long deptId = blDeliveryDocList.get(i).getDeptId();
            long userId = blDeliveryDocList.get(i).getUserId();
            BlDeliveryDoc bddQuery = new BlDeliveryDoc();
            bddQuery.setDeptId(deptId);
            bddQuery.setOrderId(orderId);
            BlDeliveryDoc blDeliveryDoc = deliveryDocMapper.findBlDeliveryAssignTimeOutUser(bddQuery);
            long psUserId = 0;
            if(blDeliveryDoc != null){
                psUserId = blDeliveryDoc.getPsuserId();
            }else{
                List<BlDeliveryDoc> deliveryDocList = deliveryDocMapper.selectfinalPsList(bddQuery);
                if(deliveryDocList.size() == 0){
                    return;
                }else{
                    psUserId = deliveryDocList.get(0).getUserId();
                }
            }
            BlDeliveryDoc ubdd = new BlDeliveryDoc();
            ubdd.setDeliveryId(deliveryId);
            ubdd.setPsuserId(psUserId);
            deliveryDocMapper.updateBlDeliveryDoc(ubdd);
            BlOrder blOrder = new BlOrder();
            blOrder.setOrderId(orderId);
            blOrder.setDeliveryId(psUserId);
            orderMapper.updateBlOrder(blOrder);
            BlDeliveryLog blDeliveryLog = new BlDeliveryLog();
            blDeliveryLog.setDeliveryId(deliveryId);
            blDeliveryLog.setDeptId(deptId);
            blDeliveryLog.setUserId(userId);
            blDeliveryLog.setOrderId(orderId);
            blDeliveryLog.setPsuserId(psUserId);
            deliveryLogMapper.insertBlDeliveryLog(blDeliveryLog);
        }
    }
}
